﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DungeonAndDragonApplication.Controller;
using System.Threading;
using DungeonAndDragonApplication.Model;
using ADNDNetwork.Network.Server;
using ADNDNetwork.Network.Client;


namespace DungeonAndDragonApplication
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            ModelManager mm = new ModelManager();
            ControllerManager cm = new ControllerManager(mm);
            
            Form1 f = new Form1();
            
            //Subscribtion  
            f.getInfo_Event += new Form1.GetInfoDelegate(cm.getAboutInformation);
            f.startGame_Event += new Form1.StartGameDelegate(cm.UserStartsGame);
            f.sendMessage_Event += new Form1.SendMessageDelegate(cm.SendMessage);
            f.receiveMessage_Event += new Form1.ReceiveMessageDelegate(cm.ReceiveMessage);


            //mm.update_Event += new ControllerManager.receivedMessageDelegate( f.update );

            
            //Thread serverThread = new Thread(StartServer(cm));
            
            Thread clientThread = new Thread(new ParameterizedThreadStart(StartClient));
            
            StartServer(cm);
            //serverThread.Start();

            clientThread.Start(cm); 

            //Application.EnableVisualStyles();
            //Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(f);
        }


        static void StartServer(ControllerManager cm)
        {
            ServerManager sm = new ServerManager(GlobalVars.IP, GlobalVars.PORT);
            sm.runGame();
        }

        static void StartClient(object obj)
        {
            Client c = new Client();
            c.ConnectToServer(GlobalVars.IP, GlobalVars.PORT);
            ClientManager.GetInstance().AddClient(c);

            /*
             * ControllerManager cm = (ControllerManager)obj;
            ClientManager.getInstance().addClient(GlobalVars.IP, GlobalVars.PORT, cm);
             */
        }
    }
}
